home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap18 / Emf11 / Emf11.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  3.4 KB  |  112 lines

  1. /*---------------------------------------
  2.    EMF11.C -- Enhanced Metafile Demo #11
  3.               (c) Charles Petzold, 1998
  4.   ---------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. TCHAR szClass [] = TEXT ("EMF11") ;
  9. TCHAR szTitle [] = TEXT ("EMF11: Enhanced Metafile Demo #11") ;
  10.  
  11. void DrawRuler (HDC hdc, int cx, int cy)
  12. {
  13.      int     i, iHeight ;
  14.      LOGFONT lf ;
  15.      TCHAR   ch ;
  16.  
  17.           // Black pen with 1-point width
  18.  
  19.      SelectObject (hdc, CreatePen (PS_SOLID, cx / 72 / 6, 0)) ;
  20.  
  21.           // Rectangle surrounding entire pen (with adjustment)
  22.  
  23.      if (GetVersion () & 0x80000000)              // Windows 98
  24.           Rectangle (hdc, 0, -2, cx + 2, cy) ;
  25.      else                                         // Windows NT
  26.           Rectangle (hdc, 0, -1, cx + 1, cy) ;
  27.  
  28.           // Tick marks
  29.           
  30.      for (i = 1 ; i < 96 ; i++)
  31.      {
  32.                if (i % 16 == 0) iHeight = cy /  2 ;    // inches
  33.           else if (i %  8 == 0) iHeight = cy /  3 ;    // half inches
  34.           else if (i %  4 == 0) iHeight = cy /  5 ;    // quarter inches
  35.           else if (i %  2 == 0) iHeight = cy /  8 ;    // eighths
  36.           else                  iHeight = cy / 12 ;    // sixteenths
  37.  
  38.           MoveToEx (hdc, i * cx / 96, 0, NULL) ;
  39.           LineTo   (hdc, i * cx / 96, iHeight) ;
  40.      }
  41.           // Create logical font 
  42.  
  43.      FillMemory (&lf, sizeof (lf), 0) ;
  44.      lf.lfHeight = cy / 2 ;
  45.      lstrcpy (lf.lfFaceName, TEXT ("Times New Roman")) ;
  46.  
  47.      SelectObject (hdc, CreateFontIndirect (&lf)) ;
  48.      SetTextAlign (hdc, TA_BOTTOM | TA_CENTER) ;
  49.      SetBkMode    (hdc, TRANSPARENT) ;
  50.  
  51.           // Display numbers
  52.  
  53.      for (i = 1 ; i <= 5 ; i++)
  54.      {
  55.           ch = (TCHAR) (i + '0') ;
  56.           TextOut (hdc, i * cx / 6, cy / 2, &ch, 1) ;
  57.      }
  58.           // Clean up
  59.  
  60.      DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
  61.      DeleteObject (SelectObject (hdc, GetStockObject (BLACK_PEN))) ;
  62. }
  63.  
  64. void CreateRoutine (HWND hwnd)
  65. {
  66.      HDC          hdcEMF ;
  67.      HENHMETAFILE hemf ;
  68.      
  69.      hdcEMF = CreateEnhMetaFile (NULL, TEXT ("emf11.emf"), NULL,
  70.                                  TEXT ("EMF11\0EMF Demo #11\0")) ;
  71.      
  72.      SetMapMode (hdcEMF, MM_LOENGLISH) ;
  73.      
  74.      DrawRuler (hdcEMF, 600, 100) ;
  75.      
  76.      hemf = CloseEnhMetaFile (hdcEMF) ;
  77.      
  78.      DeleteEnhMetaFile (hemf) ;
  79. }
  80.  
  81. void PaintRoutine (HWND hwnd, HDC hdc, int cxArea, int cyArea)
  82. {
  83.      ENHMETAHEADER emh ;
  84.      HENHMETAFILE  hemf ;
  85.      int           cxMms, cyMms, cxPix, cyPix, cxImage, cyImage ;
  86.      RECT          rect ;
  87.      
  88.      cxMms = GetDeviceCaps (hdc, HORZSIZE) ;
  89.      cyMms = GetDeviceCaps (hdc, VERTSIZE) ;
  90.      cxPix = GetDeviceCaps (hdc, HORZRES) ;
  91.      cyPix = GetDeviceCaps (hdc, VERTRES) ;
  92.      
  93.      hemf = GetEnhMetaFile (TEXT ("emf11.emf")) ;
  94.      
  95.      GetEnhMetaFileHeader (hemf, sizeof (emh), &emh) ;
  96.      
  97.      cxImage = emh.rclFrame.right - emh.rclFrame.left ;
  98.      cyImage = emh.rclFrame.bottom - emh.rclFrame.top ;
  99.      
  100.      cxImage = cxImage * cxPix / cxMms / 100 ;
  101.      cyImage = cyImage * cyPix / cyMms / 100 ;
  102.      
  103.      rect.left   = (cxArea - cxImage) / 2 ;
  104.      rect.top    = (cyArea - cyImage) / 2 ;
  105.      rect.right  = (cxArea + cxImage) / 2 ;
  106.      rect.bottom = (cyArea + cyImage) / 2 ;
  107.      
  108.      PlayEnhMetaFile (hdc, hemf, &rect) ;
  109.    
  110.      DeleteEnhMetaFile (hemf) ;
  111. }
  112.